package jou.cs.controller;

import jou.cs.configurator.CONST;
import jou.cs.entity.TComment;
import jou.cs.service.CommentService;
import jou.cs.service.TrendService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("com")
public class community {
    @Resource
    private CommentService service;
    @Resource
    private TrendService trendService;

    @GetMapping("{place}/{page}")
    public Object pp(@PathVariable Integer page, @PathVariable Long place) {
        return service.getPageByPid(place, page - 1);
    }

    @GetMapping("{page}")
    public Object gls(@PathVariable int page) {
        // select page
        return service.getCommentByPage(page - 1);
    }

    @GetMapping
    public Object gg(@RequestParam Long uid) {
        return service.getCommentByUserId(uid);
    }

    // 新增帖子
    @PostMapping
    public void add(@RequestBody TComment comment) {
        // 记录
        comment.setId(service.getMaxId() + 1);
        trendService.record(comment.getUid(), comment.getPid(), CONST.PlaceTrend.COMMENT.getType());
        service.addComment(comment);
    }

    @DeleteMapping("{id}")
    public void delete(@PathVariable Long id) {
        service.deleteComment(id);
    }

    @PutMapping
    public void update(@RequestBody TComment comment) {
        service.updateComment(comment);
    }
}
